Skip to content

Conversation

@zhongzhijie1
Copy link
Contributor

Summary

This PR adds GATT server database cache support to improve BLE reconnection experience with bonded devices. The implementation allows the stack to track GATT attribute table changes and notify peers via Service Changed indications when the database hash differs.

Changes

  • Add CONFIG_BLUETOOTH_GATTS_CACHE_SUPPORT Kconfig option to enable/disable the feature
  • Implement database hash storage and retrieval using the uv_db backend (BT_KEY_BLEGATTHASH)
  • Add SAL APIs for database hash operations and Service Changed indication
  • Track per-device GATT hash in adapter's remote device management
  • Trigger hash computation on initial BLE bonding
  • Compare stored vs. current hash on reconnection and send Service Changed if mismatched
  • Add storage migration support for the new remote_device_gatt_properties_t structure

Technical Details

  • Database hash is a 16-byte value computed from the GATT attribute table
  • Hash is stored per bonded BLE device and persists across reboots
  • On first bond, the system requests and stores the current database hash
  • On reconnection, if the stored hash differs from the current hash:
    • Service Changed indication is sent to the peer (handle range 0x0001-0xFFFF)
    • The new hash is saved to storage
  • The feature integrates with the existing bonding flow and device management

Testing

  • Tested with BLE bonding and reconnection scenarios
  • Verified hash persistence across system reboots
  • Confirmed Service Changed indication triggers on database modifications

@zhongzhijie1 zhongzhijie1 force-pushed the gatt_service_cache branch 3 times, most recently from 1fa9426 to 26a60a3 Compare December 26, 2025 10:33
@zhongzhijie1 zhongzhijie1 force-pushed the gatt_service_cache branch 3 times, most recently from 558c139 to 13d0d73 Compare January 6, 2026 15:17
zhongzhijie1 and others added 14 commits January 9, 2026 14:15
…global database hash.

bug :v/65095

The logic is implemented in gatts_service.c to trigger hash calculation
and return the result via a registered callback, for trusted device sync and DB change tracking.

Signed-off-by: zhongzhijie1 <[email protected]>
…h from adapter to profile.

bug: v/65126

Root cause:
Adapter had no generic message path to deliver events to profile services.
GATTS could not receive a request to fetch the server database hash.
Add adapter handler to forward profile_msg_t to service_manager.
Add PROFILE_EVT_GATTS_REQUEST_DB_HASH and handle it in gatts_service via process_msg.
Wire bt_sal_gatt_server_get_database_hash() call when the event is received.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65129

Root cause:
The stack did not compute and store the server GATT database hash on initial bonding.
Peers could keep a stale GATT cache because no hash update was triggered.
When a bonded LE device is connected, send PROFILE_EVT_GATTS_REQUEST_DB_HASH.
Force hash update to start cache sync for the new bond.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65131

There was no persistent storage for the server GATT database hash.
The system could not compare hashes after reboot and peers could keep a stale GATT cache.
Add remote_device_gatt_properties_t with 16-byte hash field.
Add save/load APIs for GATT hash cache using the uv_db backend (BT_KEY_BLEGATTHASH).

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65135

KVDB backend had no key and no save/load path for GATT server database hash.
The system could not restore the hash after reboot and cache validation failed.
Add BT_KVDB_BLEGATTDBHASH and helpers to save/load remote_device_gatt_properties_t entries.
Count and delete old hash properties before saving new data.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65139

The device object had no field to keep the GATT database hash.
The stack could not persist and validate GATT cache state per bonded peer.
Add a 16-byte gatt_hash field under CONFIG_BLUETOOTH_GATTS_CACHE_SUPPORT.
Add get/set/delete helpers and export remote_device_gatt_properties_t data.
Add DFLAG_GATT_HASH_VALID to mark hash presence.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65142

Adapter did not load or persist the per-device GATT database hash state.
After reboot or unpair, the stack lost hash data and cache validation became unreliable.
Load GATT hash entries on LE enable and restore them into device objects.
Save updated hash list when bond state changes and clear hash on unpair.
Use bt_storage_load_gatt_cache_device and bt_storage_save_gatt_cache_device under CONFIG_BLUETOOTH_GATTS_CACHE_SUPPORT.

Signed-off-by: zhongzhijie1 <[email protected]>
as gatts_service.

bug: v/65146

gatts_service could not access the adapter device database to read or update the stored GATT database hash.
Hash update and persistence were handled inside adapter_service only, so other modules could not drive cache sync.
Add adapter_get_device_gatt_hash() and adapter_set_device_gatt_hash() under CONFIG_BLUETOOTH_GATTS_CACHE_SUPPORT.
Only allow bonded devices and keep a zero hash when no valid hash is present.
Avoid duplicate db hash requests when DFLAG_GATT_HASH_VALID is already set.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65160

The server did not track the peer-synced GATT database hash.
Peers could keep a stale GATT cache and the stack could not detect a database change.
Request the latest database hash on GATT connection.
On hash event, compare with the stored value and decide the next action.
Update the stored hash after handling the event.
Compile the feature under CONFIG_BLUETOOTH_GATTS_CACHE_SUPPORT.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65182

Add bt_sal_gatt_server_change_indicate(start_hdl, end_hdl) with parameter validation.
Use the new SAL API from gatts_service when a hash mismatch is detected.

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65203

Signed-off-by: zhongzhijie1 <[email protected]>
bug: v/65131

Since the current version_5_0_3 has no business logic and does not impact the existing storage structure, the GATT hash is reused in version_5_0_3.

New GATT hash items added in version_5_0_3 include:
1. Added enumeration value: BT_STORAGE_UPDATE_GATT_HASH_INFO
2. Added GATT hash removal logic within the bt_storage_properties_destroy function
3. Modified the bt_storage_update_item_size table
4. Added GATT hash items to the callback_cnt_list
5. Added GATT_cache save logic to the bt_storage_update_save_info function

Signed-off-by: liuxiang18 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants